package Function;

import FCRendering.FCRBattleOrderConstant;
import FCRendering.FCRConstants;
import FCRendering.FCRMaster;
import Frame.battleanimation.*;
import Function.ItemUse.FCItemUseCenter;
import Function.ItemUse.WeaponTypeItemUse;
import VWorldGameStudio.*;
import socket.FCRSocketClient;
import socket.FCRSocketServer;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

import static FCRendering.FCRConstants.DEBUG_MODE;
import static FCRendering.FCRMaster.*;

/**
 * 战斗产生战斗指令和消费指令的地方
 */
public class FCPlayerBattleOrderHelper {

    private FCBattleOrder findTarget(FCBattleOrder order) {
        //直接找第一个未死的怪了
        int findMonsterIndex = -1;
        for (int i = 0; i < fcv.getBattleMonster().size(); i++) {
            FCMonster f = fcv.getBattleMonster().get(i);
            if (!f.getIsDeath()) {
                findMonsterIndex = i;
                break;
            }
        }
        if (findMonsterIndex == -1) {
            return null;
        }
        order.monsterNum = findMonsterIndex;
        order.battleAnis = null;
        return order;
    }

    public FCBattleOrder dealPlayerBattleOrder(FCBattleOrder order, int frames) {
        FCPlayerInfo pl = FCMutilPlayCache.findPlayerByName(order.playerUid, order.playerName);
        if (pl == null || pl.isDeath()) {
            //已经死了无法操作,丢掉当前的指令
            giveUpTheOrder();
            return null;
        }
        FCPlayerBattleWrap wrap = FCPlayerBattleWrap.findBattleWrapByName(order.playerUid, order.playerName);
        if (wrap.isInConfusion()) {
            //混乱无法操作,丢掉当前的指令
            giveUpTheOrder();
            fcv.getMessage().add(pl.getChName() + "混乱中");
            fcv.showMessage();

            fcv.startBattleInfo(pl.getChName() + "混乱中");
            return null;
        }
        if (wrap.isInSleep()) {
            //混乱无法操作,丢掉当前的指令
            giveUpTheOrder();
            fcv.getMessage().add(pl.getChName() + "睡眠中");
            fcv.showMessage();
            fcv.startBattleInfo(pl.getChName() + "睡眠中");
            return null;
        }
        if (wrap.isInParalysis()) {
            //混乱无法操作,丢掉当前的指令
            giveUpTheOrder();
            fcv.getMessage().add(pl.getChName() + "麻痹中");
            fcv.showMessage();
            fcv.startBattleInfo(pl.getChName() + "麻痹中");
            return null;
        }


        if (wrap.getBattleAttackOrder() == FCRBattleOrderConstant.Order_Run) {
            //给DefaultFightScreen整一个角色逃跑的动画
            if (frames == 5) {
                fcv.getMessage().add(pl.getChName() + "尝试逃跑...");
                fcv.showMessage();
                fcv.startBattleInfo(pl.getChName() + "尝试逃跑...");
            }
        } else if (wrap.getBattleAttackOrder() == FCRBattleOrderConstant.Order_Attack) {
            FCMonster monster = fcv.getBattleMonster().get(order.monsterNum);
            if (monster.getIsDeath()) {
                //已经死了无法操作,丢掉当前的指令
                giveUpTheOrder();
                return findTarget(order);
            }

            if (order.battleAnis == null) {
                boolean canhit = true;
                ArrayList<FCMonster> targetMs = null;
                order.battleAnis = new CopyOnWriteArrayList<>();
                int[] startpoint = new int[]{wrap.getCoorX(), wrap.getCoorY() + wrap.getH() / 2};
                Integer[] endpoint = fcv.getBattleMonsterCoor().get(order.monsterNum);
                FCAnimation fcAnimation = null;
                ABattleAni battleAni = null;
                if (wrap.getBattlePlayerSelectMenu() == FCRBattleOrderConstant.BattleSelectMenuTushou) {// 徒手攻击不能被防御
                    battleAni = new GunShotAni(animationLib.getAnimationOfID("puGong"), wrap.getName(), "puGong", startpoint, new int[]{!canhit ? 0 : (endpoint[0] + endpoint[2] / 2), endpoint[1] + endpoint[3] / 2}, true);
                    battleAni.hitFcMonsters.add(monster);
                    fcv.getMessage().add(pl.getChName() + "使出徒手攻击");
                    fcv.showMessage();
                    fcv.startBattleInfo(pl.getChName() + "使出徒手攻击");
                } else if (wrap.getBattlePlayerSelectMenu() == FCRBattleOrderConstant.BattleSelectMenuArmGun) {// 武器攻击
                    targetMs = FCItemUseCenter.getIn().findItemUseMethod(pl, null, pl.getArm(), 0, false)
                            .getHitFcMonsters(order, monster);
                    canhit = targetMs.size() > 0;
                    //这里就是不断的判断自己是什么枪了
                    FCItem fcItem = itemLib.getItemOfID(pl.getArm());
                    fcAnimation = animationLib.getAnimationOfID(fcItem.getAttackAnimation());
                    if (fcAnimation != null) {
                        if (wrap.getUseWeaponType().equals(FCRBattleOrderConstant.WT_Dao) || wrap.getUseWeaponType().equals(FCRBattleOrderConstant.WT_Qiang)
                                || wrap.getUseWeaponType().equals(FCRBattleOrderConstant.WT_HuoPao)) {
                            int[] newEndp = new int[!canhit ? 2 : targetMs.size() * 2];
                            if (!canhit) {
                                newEndp[0] = 0;
                                newEndp[1] = endpoint[1] + endpoint[3] / 2;
                            } else {
                                for (int l = 0; l < targetMs.size(); l++) {
                                    FCMonster tms = targetMs.get(l);
                                    Integer[] tm = fcv.getBattleMonsterCoor().get(fcv.getBattleMonster().indexOf(tms));
                                    newEndp[l * 2] = tm[0] + tm[2] / 2;
                                    newEndp[l * 2 + 1] = tm[1] + tm[3] / 2;
                                }
                            }
                            boolean needReady = wrap.getUseWeaponType().equals(FCRBattleOrderConstant.WT_Dao);
                            battleAni = new AttackReadyFrameModeAni(fcAnimation, startpoint, newEndp, true, needReady,
                                    wrap.getUseWeaponType().equals(FCRBattleOrderConstant.WT_HuoPao));
                        } else if (wrap.getUseWeaponType().equals(FCRBattleOrderConstant.WT_HuiXuanBiao)) {
                            battleAni = new AttackHuiXuanBiaoAni(startpoint, new int[]{!canhit ? 0 : (endpoint[0] + endpoint[2] / 2), endpoint[1] + endpoint[3] / 2}, true);
                        } else {
                            battleAni = new GunShotAni(fcAnimation, wrap.getName(), fcItem.getName(), startpoint, new int[]{!canhit ? 0 : (endpoint[0] + endpoint[2] / 2), endpoint[1] + endpoint[3] / 2}, true);
                        }
                    } else {
                        //TODO 有这枪，但没动画，咋办，使用普通攻击了
                        battleAni = new GunShotAni(animationLib.getAnimationOfID("puGong"), wrap.getName(), "puGong", startpoint, new int[]{!canhit ? 0 : (endpoint[0] + endpoint[2] / 2), endpoint[1] + endpoint[3] / 2}, true);
                    }
                    battleAni.hitFcMonsters.addAll(FCItemUseCenter.getIn()
                            .findItemUseMethod(pl, null, pl.getArm(), 0, false).getHitFcMonsters(order, monster));
                    fcv.getMessage().add(pl.getChName() + "使用了" + fcItem.getChName());
                    fcv.showMessage();
                    fcv.startBattleInfo(pl.getChName() + "使用了" + fcItem.getChName());

                } else if (wrap.getBattlePlayerSelectMenu() == FCRBattleOrderConstant.BattleSelectMenuTankMainGun
                        || wrap.getBattlePlayerSelectMenu() == FCRBattleOrderConstant.BattleSelectMenuTankFuGun
                        || wrap.getBattlePlayerSelectMenu() == FCRBattleOrderConstant.BattleSelectMenuTankSE) {
                    //TODO 主炮攻击
                    FCTank tank = tankLib.getTankOfID(pl.getPlayertank1(), pl.getPlayertankHostId());
                    if (tank != null) {
                        String gunId = tank.getGunID();
                        switch (wrap.getBattlePlayerSelectMenu()) {
                            case FCRBattleOrderConstant.BattleSelectMenuTankFuGun:
                                gunId = tank.getGun2ID();
                                break;
                            case FCRBattleOrderConstant.BattleSelectMenuTankSE:
                                gunId = tank.getSEID();
                                break;
                        }
                        targetMs = FCItemUseCenter.getIn().findItemUseMethod(pl, tank, gunId, 0, true)
                                .getHitFcMonsters(order, monster);
                        canhit = targetMs.size() > 0;
                        FCItem item = itemLib.getItemOfID(gunId);
                        fcv.getMessage().add(pl.getChName() + "使用了" + item.getChName());
                        fcv.showMessage();
                        fcv.startBattleInfo(pl.getChName() + "使用了" + item.getChName());

                        fcAnimation = animationLib.getAnimationOfID(item.getAttackAnimation());
                        if (fcAnimation != null) {
                            //TOOD 要以坦克左边中点
                            int centerX = wrap.getCoorX() + wrap.getW() / 2;
                            int tankW = (int) (tank.getImageMasterSize()[0] * fcv.getProportion() * 2);
                            int tankH = (int) (tank.getImageMasterSize()[1] * fcv.getProportion() * 2);
                            int tx = centerX - tankW / 2;
                            startpoint = new int[]{tx, wrap.getCoorY() + tankH / 2};
                            battleAni = new GunShotAni(fcAnimation, wrap.getName(), item.getName(), startpoint, new int[]{!canhit ? 0 : (endpoint[0] + endpoint[2] / 2), endpoint[1] + endpoint[3] / 2}, true);
                        } else {
                            //TODO 有这枪，但没动画，咋办，使用普通攻击
                            battleAni = new GunShotAni(animationLib.getAnimationOfID("puGong"), wrap.getName(), "puGong", startpoint, new int[]{!canhit ? 0 : (endpoint[0] + endpoint[2] / 2), endpoint[1] + endpoint[3] / 2}, true);
                        }
                        battleAni.hitFcMonsters.addAll(FCItemUseCenter.getIn().findItemUseMethod(pl, tank, gunId, 0, true)
                                .getHitFcMonsters(order, monster));
                    } else {
                        giveUpTheOrder();
                        return null;
                    }
                } else if (wrap.getBattlePlayerSelectMenu() == FCRBattleOrderConstant.BattleSelectMenuTankMainGunP) {
                    //TODO 使用炮弹,就要用炮弹的动画
                    FCTank tank = tankLib.getTankOfID(pl.getPlayertank1(), pl.getPlayertankHostId());
                    if (tank != null) {
                        FCItem dipanItem = itemLib.getItemOfID(tank.getChassisID());
                        FCItem shellItem = itemLib.getItemOfID(dipanItem.getShellCaseType()[wrap.getBattlePlayerSelectPaoDanIndex()]);
                        if (shellItem.getName().equals("PD_chouDan")) {
                            //TODO 臭弹，有8分1几率变成其他特殊弹
                            if (order.randomV1 < 0.125) {
                                shellItem = itemLib.getItemOfID(getRandomPaoDan(order.randomV3));
                                System.out.println("臭弹变成了 " + shellItem.getChName());
                            }
                        }
                        targetMs = FCItemUseCenter.getIn().findItemUseMethod(pl, tank, shellItem.getName(), 0, true)
                                .getHitFcMonsters(order, monster);
                        canhit = targetMs.size() > 0;
                        fcAnimation = animationLib.getAnimationOfID(shellItem.getAttackAnimation());
                        fcv.getMessage().add(pl.getChName() + "进行发射" + shellItem.getChName());
                        fcv.showMessage();
                        fcv.startBattleInfo(pl.getChName() + "进行发射" + shellItem.getChName());
                        if (fcAnimation != null) {
                            //TOOD 要以坦克左边中点
                            int centerX = wrap.getCoorX() + wrap.getW() / 2;
                            int tankW = (int) (tank.getImageMasterSize()[0] * fcv.getProportion() * 2);
                            int tankH = (int) (tank.getImageMasterSize()[1] * fcv.getProportion() * 2);
                            int tx = centerX - tankW / 2;
                            startpoint = new int[]{tx, wrap.getCoorY() + tankH / 2};
                            battleAni = new GunShotAni(fcAnimation, wrap.getName(), shellItem.getName(), startpoint, new int[]{!canhit ? 0 : (endpoint[0] + endpoint[2] / 2), endpoint[1] + endpoint[3] / 2}, true);
                        } else {
                            battleAni = new GunShotAni(animationLib.getAnimationOfID("puGong"), wrap.getName(), "puGong", startpoint, new int[]{!canhit ? 0 : (endpoint[0] + endpoint[2] / 2), endpoint[1] + endpoint[3] / 2}, true);
                        }
                        battleAni.hitFcMonsters.addAll(FCItemUseCenter.getIn().findItemUseMethod(pl, tank, dipanItem.getShellCaseType()[wrap.getBattlePlayerSelectPaoDanIndex()], 0, true)
                                .getHitFcMonsters(order, monster));
                    } else {
                        giveUpTheOrder();
                        return null;
                    }
                }
                if (battleAni != null) {
                    order.battleAnis.add(battleAni);
                    order.frameTimes = battleAni.getAllFrames();
                }
            }
        } else if (wrap.getBattleAttackOrder() == FCRBattleOrderConstant.Order_USEITEM) {
            if (order.battleAnis == null) {
                order.battleAnis = new CopyOnWriteArrayList<>();
                if (wrap.getBattleUseItemType() == 1) {
                    FCMonster monster = fcv.getBattleMonster().get(order.monsterNum);
                    //对敌人使用
                    if (monster.getIsDeath()) {
                        //已经死了无法操作,丢掉当前的指令
                        giveUpTheOrder();
                        return findTarget(order);
                    }
                    boolean canhit = false;

                    String fcItemName = pl.getKnapsackItemIDOfNum(wrap.getBattlePlayerSelectItem());
                    FCTank tank = tankLib.getTankOfID(pl.getPlayertank1(), pl.getPlayertankHostId());
                    if (wrap.getBattlePlayerSelectItemMode() == 2) {
                        if (tank != null) {
                            fcItemName = tank.getKnapsackItemIDOfNum(wrap.getBattlePlayerSelectItem());
                        }
                    }
                    FCItem doItem = itemLib.getItemOfID(fcItemName);
                    if (doItem == null) {
                        giveUpTheOrder();
                        return null;
                    }
                    if (doItem.getType().equals("tool") || doItem.getType().equals("plate")) {
                        //使用工具应该是原地使用
                        ABattleAni attackAni = new UseToolAnimation(fcItemName, new int[]{wrap.getCoorX() + wrap.getW() / 2, wrap.getCoorY()}, true);
                        order.battleAnis.add(attackAni);
                        order.frameTimes = attackAni.getAllFrames();
                        fcv.getMessage().add(pl.getChName() + "开始使用" + doItem.getChName());
                        fcv.showMessage();
                        fcv.startBattleInfo(pl.getChName() + "开始使用" + doItem.getChName());
                    } else {//投掷物，食物，饮料的动画
                        ArrayList<FCMonster> tMs = FCItemUseCenter.getIn().findItemUseMethod(pl, tank, fcItemName, wrap.getBattlePlayerSelectItem(), wrap.getBattlePlayerSelectItemMode() == 2)
                                .getHitFcMonsters(order, monster);
                        canhit = tMs.size() > 0;

                        Integer[] mxy = fcv.getBattleMonsterCoor().get(order.monsterNum);
                        int[] endpoint = new int[]{(int) (mxy[0] + mxy[2] / 3), mxy[1] + mxy[3] / 2};
                        if (!canhit) {
                            endpoint[0] = 0;
                        }
                        ABattleAni attackAni = new ThrowItemAni(fcItemName, true, wrap.getName(), new int[]{(int) (wrap.getCoorX() + wrap.getW() - 8 * fcv.getProportion()), (int) (wrap.getCoorY() + 8 * fcv.getProportion())}
                                , endpoint, true);
                        order.battleAnis.add(attackAni);
                        order.frameTimes = attackAni.getAllFrames();
                        attackAni.hitFcMonsters.add(monster);
                        fcv.getMessage().add(pl.getChName() + " 投掷" + doItem.getChName());
                        fcv.showMessage();
                        fcv.startBattleInfo(pl.getChName() + " 投掷" + doItem.getChName());
                    }
                } else {
                    //对自己人使用
                    String[] targetS = wrap.getBattlePlayerSelectPlayer().split(",");
                    FCPlayerBattleWrap tagetWrap = FCPlayerBattleWrap.findBattleWrapByName(targetS[0], targetS[1]);
                    FCPlayerInfo tagetPlayer = FCMutilPlayCache.findPlayerByName(targetS[0], targetS[1]);
                    if (tagetPlayer != null && !tagetPlayer.isDeath()) {
                        String fcItemName = pl.getKnapsackItemIDOfNum(wrap.getBattlePlayerSelectItem());
                        if (wrap.getBattlePlayerSelectItemMode() == 2) {
                            FCTank tank = tankLib.getTankOfID(pl.getPlayertank1(), pl.getPlayertankHostId());
                            if (tank == null) {
                                fcItemName = tank.getKnapsackItemIDOfNum(wrap.getBattlePlayerSelectItem());
                            }
                        }
                        FCItem doItem = itemLib.getItemOfID(fcItemName);
                        if (doItem == null) {
                            giveUpTheOrder();
                            return null;
                        }
                        fcv.getMessage().add(pl.getChName() + "进行使用" + doItem.getChName());
                        fcv.showMessage();
                        fcv.startBattleInfo(pl.getChName() + "进行使用" + doItem.getChName());
                        if (doItem.getType().equals("throw")) {
                            ABattleAni attackAni = new ThrowItemAni(fcItemName, false, wrap.getName(), new int[]{(int) (wrap.getCoorX() + wrap.getW() - 8 * fcv.getProportion()), (int) (wrap.getCoorY() + 8 * fcv.getProportion())}
                                    , new int[]{(int) (tagetWrap.getCoorX() + tagetWrap.getW() / 3), tagetWrap.getCoorY() + tagetWrap.getH() / 2}, true);
                            order.battleAnis.add(attackAni);
                            order.frameTimes = attackAni.getAllFrames();
                        } else {
                            //使用工具应该是原地使用
                            ABattleAni attackAni = new UseToolAnimation(fcItemName, new int[]{wrap.getCoorX() + wrap.getW() / 2, wrap.getCoorY()}, true);
                            order.battleAnis.add(attackAni);
                            order.frameTimes = attackAni.getAllFrames();
                        }
                    } else {
                        giveUpTheOrder();
                        //TODO 想要使用道具，但人物已经死亡
                        fcv.getMessage().add(
                                FCRConstants.SYSTEM + FCRConstants.BATTLE + wrap.getName() + " 使用道具失败，使用对象已死亡！");
                        return null;
                    }
                }
            }
        } else if (wrap.getBattleAttackOrder() == FCRBattleOrderConstant.Order_Defense) {
            //TODO 防御
            if (order.battleAnis == null) {
                FangyuAni fangyuAni = new FangyuAni(wrap.getName(), null, new int[]{wrap.getCoorX(), wrap.getCoorY() + wrap.getH() / 2}, true);
                order.battleAnis = new CopyOnWriteArrayList<>();
                order.battleAnis.add(fangyuAni);
                order.frameTimes = fangyuAni.getAllFrames();
                fcv.getMessage().add(pl.getChName() + "展开了防御");
                fcv.showMessage();
                fcv.startBattleInfo(pl.getChName() + "展开了防御");
            }
        } else if (wrap.getBattleAttackOrder() == FCRBattleOrderConstant.Order_Protect) {
            //TODO 保护
            if (order.battleAnis == null) {
                FangyuAni fangyuAni = new FangyuAni(wrap.getName(), wrap.getBattlePlayerSelectPlayer().split(",")[1], new int[]{wrap.getCoorX(), wrap.getCoorY() + wrap.getH() / 2}, true);
                order.battleAnis = new CopyOnWriteArrayList<>();
                order.battleAnis.add(fangyuAni);
                order.frameTimes = fangyuAni.getAllFrames();
                fcv.getMessage().add(pl.getChName() + "展开了保护");
                fcv.showMessage();
                fcv.startBattleInfo(pl.getChName() + "展开了保护");
            }
        } else if (wrap.getBattleAttackOrder() == FCRBattleOrderConstant.Order_EQUIP) {
            //TODO  操作换装备,比较特殊
            if (order.battleAnis == null) {
                FangyuAni fangyuAni = new FangyuAni(wrap.getName(), null, new int[]{wrap.getCoorX(), wrap.getCoorY() + wrap.getH() / 2}, true);
                order.battleAnis = new CopyOnWriteArrayList<>();
                order.battleAnis.add(fangyuAni);
                order.frameTimes = fangyuAni.getAllFrames();
                fcv.getMessage().add(pl.getChName() + "更换装备");
                fcv.showMessage();
                fcv.startBattleInfo(pl.getChName() + "更换装备");
                if (wrap.getHostUid().equals(gameSave.HOST_UID)) {
                    changePlayerEquip(wrap, pl);
                }
            }
        }

        if (order.battleAnis != null) {
            for (ABattleAni battleAni : order.battleAnis) {
                battleAni.nextFrame(frames);
            }
        }
        return null;
    }


    private void changePlayerEquip(FCPlayerBattleWrap localWrap, FCPlayerInfo localPlayer) {

        //脱装备
        FCPlayerInfo changeArmorMan = FCMutilPlayCache.findPlayerByName(gameSave.HOST_UID, fcv.getKnapsackItemWho());
        System.out.println("换装备 换装人："+changeArmorMan.getChName()+" 背包人:"+localPlayer.getChName()+
                " 行动类型："+localWrap.getBattlePlayerSelectMenu()+" 背包类型："+localWrap.getBattlePlayerSelectItemMode()+
                " 装备类型："+localWrap.getTakeOffArmorType()+" 背包位置："+localWrap.getKnapsackItemNum());
        if (localWrap.getBattlePlayerSelectMenu() == FCRBattleOrderConstant.BattleSelectTakeOffArmor) {
            if (localWrap.getBattlePlayerSelectItemMode() == 1) {
                //脱到人类背包
                if (localWrap.getTakeOffArmorType() == 0) {
                    localPlayer.insertItemToKnapsack(localWrap.getKnapsackItemNum(), changeArmorMan.getArm());
                    changeArmorMan.setArm("null");
                } else if (localWrap.getTakeOffArmorType() == 1) {
                    localPlayer.insertItemToKnapsack(localWrap.getKnapsackItemNum(), changeArmorMan.getHat());
                    changeArmorMan.setHat("null");
                } else if (localWrap.getTakeOffArmorType() == 2) {
                    localPlayer.insertItemToKnapsack(localWrap.getKnapsackItemNum(), changeArmorMan.getJacket());
                    changeArmorMan.setJacket("null");
                } else if (localWrap.getTakeOffArmorType() == 3) {
                    localPlayer.insertItemToKnapsack(localWrap.getKnapsackItemNum(), changeArmorMan.getPants());
                    changeArmorMan.setPants("null");
                } else if (localWrap.getTakeOffArmorType() == 4) {
                    localPlayer.insertItemToKnapsack(localWrap.getKnapsackItemNum(), changeArmorMan.getShoes());
                    changeArmorMan.setShoes("null");
                } else if (localWrap.getTakeOffArmorType() == 5) {
                    localPlayer.insertItemToKnapsack(localWrap.getKnapsackItemNum(), changeArmorMan.getGlove());
                    changeArmorMan.setGlove("null");
                }
                if (localWrap.getTakeOffArmorType() >= 0) {
                    syncEquipToNet(changeArmorMan);
                }
            } else {
                FCTank tank = tankLib.getTankOfID(localPlayer.getPlayertank1(), localPlayer.getPlayertankHostId());
                if(tank != null) {
                    //  脱到坦克背包
                    if (localWrap.getTakeOffArmorType() == 0) {
                        tank.insertItemToKnapsack(localWrap.getKnapsackItemNum(), changeArmorMan.getArm());
                        changeArmorMan.setArm("null");
                    } else if (localWrap.getTakeOffArmorType() == 1) {
                        tank.insertItemToKnapsack(localWrap.getKnapsackItemNum(), changeArmorMan.getHat());
                        changeArmorMan.setHat("null");
                    } else if (localWrap.getTakeOffArmorType() == 2) {
                        tank.insertItemToKnapsack(localWrap.getKnapsackItemNum(), changeArmorMan.getJacket());
                        changeArmorMan.setJacket("null");
                    } else if (localWrap.getTakeOffArmorType() == 3) {
                        tank.insertItemToKnapsack(localWrap.getKnapsackItemNum(), changeArmorMan.getPants());
                        changeArmorMan.setPants("null");
                    } else if (localWrap.getTakeOffArmorType() == 4) {
                        tank.insertItemToKnapsack(localWrap.getKnapsackItemNum(), changeArmorMan.getShoes());
                        changeArmorMan.setShoes("null");
                    } else if (localWrap.getTakeOffArmorType() == 5) {
                        tank.insertItemToKnapsack(localWrap.getKnapsackItemNum(), changeArmorMan.getGlove());
                        changeArmorMan.setGlove("null");
                    }
                    tank.reInitShuxing();
                    if (localWrap.getTakeOffArmorType() >= 0) {
                        syncEquipToNet(changeArmorMan);
                    }
                }else {
                    fcv.getMessage().add(localPlayer.getChName() + "更换装备失败，坦克不见了");
                    fcv.showMessage();
                    fcv.startBattleInfo(localPlayer.getChName() + "更换装备，坦克不见了");
                }
            }
        } else if (localWrap.getBattlePlayerSelectMenu() == FCRBattleOrderConstant.BattleSelectTakeUpArmor) {
            //换装备
            String willTakeArmor = null;
            if (localWrap.getBattlePlayerSelectItemMode() == 1) {
                willTakeArmor = localPlayer.getKnapsackItemIDOfNum(localWrap.getKnapsackItemNum());
            }else {
                FCTank tank = tankLib.getTankOfID(localPlayer.getPlayertank1(), localPlayer.getPlayertankHostId());
                if(tank != null) {
                    willTakeArmor = tank.getKnapsackItemIDOfNum(localWrap.getKnapsackItemNum());
                }
            }
            String oldItemName = "null";
            if(willTakeArmor != null) {
                if (localWrap.getTakeOffArmorType() == 0) {
                    oldItemName = changeArmorMan.getArm();
                    changeArmorMan.setArm(willTakeArmor);
                } else if (localWrap.getTakeOffArmorType() == 1) {
                    oldItemName = changeArmorMan.getArm();
                    changeArmorMan.setHat(willTakeArmor);
                } else if (localWrap.getTakeOffArmorType() == 2) {
                    oldItemName = changeArmorMan.getJacket();
                    changeArmorMan.setJacket(willTakeArmor);
                } else if (localWrap.getTakeOffArmorType() == 3) {
                    oldItemName = changeArmorMan.getPants();
                    changeArmorMan.setPants(gameSave.checkOtherItem(willTakeArmor));
                } else if (localWrap.getTakeOffArmorType() == 4) {
                    oldItemName = changeArmorMan.getShoes();
                    changeArmorMan.setShoes(willTakeArmor);
                } else if (localWrap.getTakeOffArmorType() == 5) {
                    oldItemName = changeArmorMan.getGlove();
                    changeArmorMan.setGlove(willTakeArmor);
                }
                if (localWrap.getBattlePlayerSelectItemMode() == 1) {
                    //人类背包
                    localPlayer.insertItemToKnapsack(localPlayer.getKnapsackItemNum(), oldItemName);
                }else {
                    FCTank tank = tankLib.getTankOfID(localPlayer.getPlayertank1(), localPlayer.getPlayertankHostId());
                    if(tank != null) {
                        tank.insertItemToKnapsack(localPlayer.getKnapsackItemNum(), oldItemName);
                        tank.reInitShuxing();
                    }
                }
                syncEquipToNet(changeArmorMan);
            }else {
                fcv.getMessage().add(localPlayer.getChName() + "更换装备失败，装备找不到");
                fcv.showMessage();
                fcv.startBattleInfo(localPlayer.getChName() + "更换装备，装备找不到");
            }
        }
    }


    private void syncEquipToNet(FCPlayerInfo p) {
        if (fcv.isSever()) {
            FCRSocketServer.getIn().syncPlayerEquip(p);
        } else if (fcv.isClient()) {
            FCRSocketClient.getIn().syncPlayerEquip(p);
        }
    }

    private void giveUpTheOrder() {
        fcv.getBattleAttackOrder().pop();
        fcv.setBattleAnimationNum(0);
    }

    //打人结束
    public FCBattleOrder dealPlayerBattleOrderEnd(FCBattleOrder order) {
        FCPlayerInfo pl = FCMutilPlayCache.findPlayerByName(order.playerUid, order.playerName);
        if (pl == null || pl.isDeath()) {
            //已经死了无法操作
            return null;
        }
        FCPlayerBattleWrap wrap = FCPlayerBattleWrap.findBattleWrapByName(order.playerUid, order.playerName);
        FCMonster monster = null;
        if (order.monsterNum >= 0 && order.monsterNum < fcv.getBattleMonster().size()) {
            monster = fcv.getBattleMonster().get(order.monsterNum);
        }
        FCTank fcTank = tankLib.getTankOfID(pl.getPlayertank1(), pl.getPlayertankHostId());
        FCItem item = null;
        if (wrap.getBattleAttackOrder() == FCRBattleOrderConstant.Order_Run) {
            //逃跑
            double jilv = 0;// 逃跑几率
            /**
             * 公式式，当pl.getSpeed1() + pl.getIntelligence() = 50时要有0.35， =94时有0.9得出
             * a = 0.00714  c=0.0428
             */
            jilv = 0.00714 * (pl.getSpeed1() + pl.getIntelligence()) + 0.0428;
            if (DEBUG_MODE) {
                jilv = 10000;//逃跑调试
            }
            if (order.randomV4 <= jilv) {
                fcv.getBattleAttackOrder().clear();
                fcv.getMessage().add(pl.getChName() + "逃跑成功");
                fcv.showMessage();
                fcv.setBattlePlayerOneSelectMonster(-1);
                fcv.setBattleSummary(true);
                fcv.endBattleInfo(pl.getChName() + "逃跑成功");
                return null;
            } else {
                fcv.endBattleInfo(pl.getChName() + "逃跑失败...");
                fcv.getMessage().add(pl.getChName() + "逃跑失败...");
            }
        } else if (wrap.getBattleAttackOrder() == FCRBattleOrderConstant.Order_Attack) {// 攻击
            if (wrap.getBattlePlayerSelectMenu() == FCRBattleOrderConstant.BattleSelectMenuTushou) {// 徒手攻击
                //TODO 徒手攻击不能被闪避
                boolean isBaoJi = false;
                int attckHurt = (int) (pl.getStrength() * 1.25);//徒手就用强度了
                int cirt = pl.getCritRate(wrap);//暴击值（最大100）
                int addAttack = wrap.getLongTermVal(FCRBattleOrderConstant.S_AddAttack);
                if (addAttack != -1) {
                    attckHurt += addAttack;
                }
                if (order.randomV3 * 100 < cirt) {//产生了暴击
                    attckHurt += attckHurt;
                    isBaoJi = true;
                }
                //TODO 徒手攻击就不要计算装甲了
                int defense = monster.getLongTermVal(FCRBattleOrderConstant.S_Defense);
                defense = defense == -1 ? 0 : defense;
                defense += monster.getDefense();
                attckHurt = attckHurt - defense;
                attckHurt = Math.max(attckHurt, 1);
                int msBlood = monster.getBlood() - attckHurt;
                msBlood = Math.max(0, msBlood);
                monster.setBlood(msBlood);
                fcv.getMessage().add((fcv.getBattleMonster().indexOf(monster) + 1) + "号" + monster.getChName() + " HP减" + msBlood + (isBaoJi ? "~会心一击" : ""));
                fcv.showMessage();

                fcv.endBattleInfo((fcv.getBattleMonster().indexOf(monster) + 1) + "号" + monster.getChName() + " HP减" + msBlood + (isBaoJi ? "~会心一击" : ""));
            } else if (wrap.getBattlePlayerSelectMenu() == FCRBattleOrderConstant.BattleSelectMenuArmGun) {// 武器攻击
                FCItemUseCenter.getIn().findItemUseMethod(pl, fcTank, pl.getArm(), 0, false)
                        .useForMonsterTarget(order, monster);
            } else if (wrap.getBattlePlayerSelectMenu() == FCRBattleOrderConstant.BattleSelectMenuTankMainGun) {
                //TODO 主炮攻击
                if (fcTank != null)
                    FCItemUseCenter.getIn().findItemUseMethod(pl, fcTank, fcTank.getGunID(), 0, true)
                            .useForMonsterTarget(order, monster);
            } else if (wrap.getBattlePlayerSelectMenu() == FCRBattleOrderConstant.BattleSelectMenuTankFuGun) {
                //TODO 副炮
                if (fcTank != null)
                    FCItemUseCenter.getIn().findItemUseMethod(pl, fcTank, fcTank.getGun2ID(), 0, true)
                            .useForMonsterTarget(order, monster);
            } else if (wrap.getBattlePlayerSelectMenu() == FCRBattleOrderConstant.BattleSelectMenuTankSE) {
                //TODO SE攻击
                if (fcTank != null)
                    FCItemUseCenter.getIn().findItemUseMethod(pl, fcTank, fcTank.getSEID(), 0, true)
                            .useForMonsterTarget(order, monster);
            } else if (wrap.getBattlePlayerSelectMenu() == FCRBattleOrderConstant.BattleSelectMenuTankMainGunP) {
                //TODO 使用炮弹攻击
                if (fcTank != null) {
                    FCItem dipanItem = itemLib.getItemOfID(fcTank.getChassisID());
                    FCItem shellItem = itemLib.getItemOfID(dipanItem.getShellCaseType()[wrap.getBattlePlayerSelectPaoDanIndex()]);
                    if (shellItem.getName().equals("PD_chouDan")) {
                        //TODO 臭弹，有8分1几率变成其他特殊弹
                        if (order.randomV1 < 0.125) {
                            shellItem = itemLib.getItemOfID(getRandomPaoDan(order.randomV3));
                        }
                    }
                    FCItemUseCenter.getIn().findItemUseMethod(pl, fcTank, shellItem.getName(), 0, true)
                            .useForMonsterTarget(order, monster);
                }
            }
        } else if (wrap.getBattleAttackOrder() == FCRBattleOrderConstant.Order_USEITEM) {
            pl.addPhysicalOfNum(-1);// 操作消耗体力
            if (wrap.getBattlePlayerSelectItemMode() == 2) {
                item = itemLib.getItemOfID(fcTank.getKnapsackItemIDOfNum(wrap.getBattlePlayerSelectItem()));
            } else {
                item = itemLib.getItemOfID(pl.getKnapsackItemIDOfNum(wrap.getBattlePlayerSelectItem()));
            }
            //使用道具
            if (wrap.getBattleUseItemType() == 1) {//对敌人使用
                FCMonster fcMonster = fcv.getBattleMonster().get(order.monsterNum);
                if (item.getType().equals("throw") || item.getType().equals("tool")) {
                    FCItemUseCenter.getIn().findItemUseMethod(pl, fcTank, item.getName(), wrap.getBattlePlayerSelectItem(), wrap.getBattlePlayerSelectItemMode() == 2)
                            .useForMonsterTarget(order, fcMonster);
                } else {
                    //TODO 这里操作是否给怪物回血，无效
                    fcv.getMessage().add(" 什么也没发生");
                    fcv.endBattleInfo(" 什么也没发生");
                }
            } else if (wrap.getBattleUseItemType() == 2) {
                String[] splits = wrap.getBattlePlayerSelectPlayer().split(",");
                FCPlayerInfo targetP = FCMutilPlayCache.findPlayerByName(splits[0], splits[1]);
                if (item.getType().equals("throw")) {
                    //TODO 这里操作是否给人物造成伤害
                    fcv.getMessage().add(" 什么也没发生");
                    fcv.endBattleInfo(" 什么也没发生");
                } else {
                    if (item.getCatalog().equals("tank")) {
                        FCTank targetTank = tankLib.getTankOfID(targetP.getPlayertank1(), targetP.getPlayertankHostId());
                        if (targetTank != null) {
                            FCItemUseCenter.getIn().findItemUseMethod(pl, targetTank, item.getName(), wrap.getBattlePlayerSelectItem(), wrap.getBattlePlayerSelectItemMode() == 2)
                                    .useForTankTarget(targetTank);
                        } else {
                            fcv.getMessage().add(" 什么也没发生");
                            fcv.endBattleInfo(" 什么也没发生");
                        }
                    } else {
                        FCItemUseCenter.getIn().findItemUseMethod(pl, fcTank, item.getName(), wrap.getBattlePlayerSelectItem(), wrap.getBattlePlayerSelectItemMode() == 2)
                                .useForPlayerTarget(targetP);
                    }
                }
            }
        } else if (wrap.getBattleAttackOrder() == FCRBattleOrderConstant.Order_Defense) {
            //TODO  个人增加防御1回合，增加的防御是个人强度*1.25吧，1回合后无效
            int attckDefense = (int) (pl.getStrength() * 1.25);
            wrap.addlongTermState(FCRBattleOrderConstant.S_Defense, attckDefense, 2);
        } else if (wrap.getBattleAttackOrder() == FCRBattleOrderConstant.Order_Protect) {
            //TODO  个人保护防御1回合，增加的防御是个人强度*1.25吧，1回合后无效
            int attckDefense = (int) (pl.getStrength() * 1.25);
            String[] targetName = wrap.getBattlePlayerSelectPlayer().split(",");
            FCPlayerBattleWrap target = FCPlayerBattleWrap.findBattleWrapByName(targetName[0], targetName[1]);
            target.addlongTermState(FCRBattleOrderConstant.S_Defense, attckDefense, 2);
        } else if (wrap.getBattleAttackOrder() == FCRBattleOrderConstant.Order_EQUIP) {
            //TODO  操作换装备,比较特殊
        }
        return null;
    }

    public FCBattleOrder createPlayerBattleOrder(FCPlayerInfo playerInfo, FCPlayerBattleWrap wrap) {
        FCBattleOrder order = new FCBattleOrder();
        order.playerUid = wrap.getHostUid();
        order.playerName = wrap.getName();
        order.monsterNum = wrap.getBattlePlayerSelectMonster();
        order.battleSpeed = playerInfo.getBattleSpeed(wrap);
        order.battleType = 1;
        return order;
    }


    private String getRandomPaoDan(float randSeed) {
        String[] strings = new String[]{"PD_zhaDan", "PD_yanMuDan", "PD_tieDan", "PD_sanDan", "PD_qiYouDan",
                "PD_shellnormal", "PD_liuDan", "PD_gangDan", "PD_duQiDan", "PD_cuiMianDan", "PD_bingDan", "PD_shuiNiDan"};
        int s = Math.min(strings.length - 1, (int) (randSeed * strings.length));
        return strings[s];
    }
}
